'use strict'
import { _xhb_map_loadScript, _xhb_map_waitForParameterReady, _xhb_map_loadCss } from '../../../utils/util.js'

class _BMapComponent {
    constructor(opt) {
        this.options = opt;
    }
    initMap() {
        window.BMAP_PROTOCOL = "https";
        window.BMapGL_loadScriptTime = (new Date).getTime();
        _xhb_map_loadCss("https://api.map.baidu.com/res/webgl/10/bmap.css")
        _xhb_map_loadScript(`http://api.map.baidu.com/getscript?type=webgl&v=1.0&ak=${this.options.security.code}&services=&t=20240801100837`, () => {
            // GL版命名空间为BMapGL
            // 按住鼠标右键，修改倾斜角和角度
            window.globalMap.map = new BMapGL.Map(this.options.mapContainer);
            console.log('百度地图初始化成功')
            window.globalMap.status = true;
            window.globalMap.map.centerAndZoom(new BMapGL.Point(this.options.center[0], this.options.center[1]), this.options.zoom);  // 初始化地图,设置中心点坐标和地图级别
            window.globalMap.map.enableScrollWheelZoom(true);     //开启鼠标滚轮缩放
            if (this.options.mapStyle) {
                switch (this.options.mapStyle) {
                    case 'BMAP_SATELLITE_MAP':
                        window.globalMap.map.setMapType(BMAP_SATELLITE_MAP);
                        break;
                    case 'BMAP_EARTH_MAP':
                        window.globalMap.map.setMapType(BMAP_EARTH_MAP);
                        break;
                    case 'BMAP_SATELLITE_MAP':
                        window.globalMap.map.setMapType(BMAP_SATELLITE_MAP);
                        break;
                    default:
                        window.globalMap.map.setMapStyleV2({
                            styleId: this.options.mapStyle
                        });
                }
            }
        })
    }

    addMarker(id, options, markerEvent) {
        var point = new BMapGL.Point(options.position[0], options.position[1]);
        let marker = new BMapGL.Marker(point);
        marker.addEventListener(markerEvent.on, markerEvent.callBack);
        window.globalMap.map.addOverlay(marker);                     // 将标注添加到地图中
        window.globalMap.markers.push({ id, data:marker });
    }

    delMarker(id) {
        window.globalMap.markers.filter(o => o.id === id).forEach(o => window.globalMap.map.removeOverlay(o.data))
    }

    addPolyline(id,options,markerEvent){

    }

    delPolyline(id){
        window.globalMap.polyline.filter(o => o.id === id).forEach(o => window.globalMap.map.remove(o.data))
    }
}

export default _BMapComponent